package com.nerve.android.cloudoffice.app.activity.customer;

import java.util.ArrayList;
import java.util.List;

import org.nerve.android.NerveActivityGroup;
import org.nerve.android.annotation.Acvitity;
import org.nerve.android.annotation.ViewOnId;
import org.nerve.util.DateUtil;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.nerve.android.cloudoffice.OfficeApplication;
import com.nerve.android.cloudoffice.R;
import com.nerve.android.cloudoffice.app.NavigationActivity;
import com.nerve.android.cloudoffice.app.group.IndexGroupActivity;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.common.Reply;
import com.nerve.android.cloudoffice.common.Resource;
import com.nerve.android.cloudoffice.domain.Customer;
import com.nerve.android.cloudoffice.service.data.CustomerData;

@SuppressLint("HandlerLeak")
@Acvitity(layout=R.layout.activity_customer)
public class CustomerActivity extends NavigationActivity implements OnClickListener{
	
	private int selectType;
	private TextView[] picker;
	
	@ViewOnId(id=R.id.tableLayout)
	private LinearLayout tableLayout;
	
	private List<Customer> customerList = new ArrayList<Customer>();
	
	private OnClickListener customerClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			int index = v.getId();
			Customer c = customerList.get(index);
			
			putData(AddCustomerActivity.CUSTOMER, c);
			
			Intent intent = new Intent(CustomerActivity.this, AddCustomerActivity.class);
			startActivityForResult(intent, 0);
		}
	};
	
	
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			hideProgressDialog();
			switch (msg.what) {
			case WORKER_OK:
				buildListData();
				break;
			case WORKER_FAIL:
				toast(Const.ERROR);
				break;
			}
		}
	};
	
	@Override
	protected void initUI() {
		super.initUI();
		
		picker = new TextView[4];
		picker[0] = (TextView)findViewById(R.id.newCustomer);
		picker[1] = (TextView)findViewById(R.id.toubiao);
		picker[2] = (TextView)findViewById(R.id.zhongdian);
		picker[3] = (TextView)findViewById(R.id.completed);
		
		bindClick();
		
		setNavTitle("客户关系");
		customNavigateRightButton("增加客户", true);
	}
	
	@Override
	protected void onNavigateBackButtonClick() {
		NerveActivityGroup group = getActivityGroup(IndexGroupActivity.class);
		
		group.changeTab(IndexGroupActivity.HOME);
	}
	
	@Override
	protected void onNavigateRightButtonClick() {
		Intent intent = new Intent(CustomerActivity.this, AddCustomerActivity.class);
		startActivityForResult(intent, 0);
	}
	
	private void bindClick(){
		for(int i=0;i<picker.length;i++)
			picker[i].setOnClickListener(this);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		new CustomerWorker().execute();
	}

	@Override
	public void onClick(View v) {
		int type = -1;
		switch (v.getId()) {
		case R.id.newCustomer:
			type = Customer.NEW;
			break;
		case R.id.toubiao:
			type = Customer.BID;
			break;
		case R.id.zhongdian:
			type = Customer.IMPORTANT;
			break;
		case R.id.completed:
			type = Customer.COMPLETE;
			break;
		}
		
		if(type == selectType)
			return ;
		
		selectType = type;
		changeTab();
		new CustomerWorker().execute();
	}
	
	private void changeTab(){
		for(TextView tv : picker){
			tv.setTextColor(getResources().getColor(R.color.tab_select_no));
		}
		
		picker[selectType].setTextColor(getResources().getColor(R.color.tab_select));
	}
	
	private void buildListData(){
		tableLayout.removeAllViews();
		
		LayoutParams lpp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		lpp.setMargins(0, 1, 0, 0);
		
		LinearLayout view = null;
		for(int i=0;i<customerList.size();i++){
			Customer customer = customerList.get(i);
			
			view = (LinearLayout)View.inflate(this, R.layout.activity_customer_item, null);
			
			((TextView)view.findViewById(R.id.time)).setText(
					DateUtil.toString(customer.getStartDate(), "MM-dd")
					);
			
			((TextView)view.findViewById(R.id.place)).setText(customer.getAddress());
			((TextView)view.findViewById(R.id.lianxiren)).setText(customer.getName());
			((TextView)view.findViewById(R.id.project)).setText(
					Resource.Customer.PROJECT_TYPE[customer.getProjectType()]
					);
			
			view.setId(i);
			view.setOnClickListener(customerClickListener);
			
			tableLayout.addView(view, lpp);
		}
	}
	
	private class CustomerWorker extends AsyncTask<Void, Void, Integer>{

		@SuppressWarnings("unchecked")
		@Override
		protected Integer doInBackground(Void... params) {
			Reply re = CustomerData.list(OfficeApplication.getLoginId(), selectType);
			
			if(re.isError()){
				Const.ERROR = re.getMessage().toString();
				return WORKER_FAIL;
			}
			
			customerList = (List<Customer>)re.getMessage();
			
			return WORKER_OK;
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			handler.sendEmptyMessage(result);
		}
	}
}